var http = require('../../utils/http.js')
var util = require('../../utils/util.js')
var localstorage = require('../../utils/localstorage.js')
var appInstance = getApp()
var page = 1;
var pageSize = 15;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    selectZhigan: {
      id: 0,
      title: "所有质感"
    },
    selsectColor: {
      id: 0,
      title: "所有颜色"
    },
    selectCar: {
      id: 0,
      title: "所有车型"
    },
    selectXiLie: {
      id: 0,
      typename: "所有系列"
    },
    ZhiganArr: [],
    ColorArr: [],
    CarArr: [],
    SubCarArr: [],
    XiLieArr: [],
    //
    datalist: [],
    isMore: true,
    searchWork: "",
    isloading: false,

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {


    this.getdatalist(
      this.data.selectZhigan,
      this.data.selsectColor,
      this.data.selectCar,
    );
    this.getzhiganlist();
    this.getColor2list();
    this.getCarlist();
    //this.getXiLielist();

  },

 

  onReachBottom() {
    if (this.data.isMore) {
      this.setData({
        isloading: true
      })
      page = page + 1;
      this.getdatalist(
        this.data.selectZhigan,
        this.data.selsectColor,
        this.data.selectCar,
      );
    }
  },

  onPullDownRefresh() {
    this.setData({
      datalist: [],
    })
    page = 1;
    this.getdatalist(
      this.data.selectZhigan,
      this.data.selsectColor,
      this.data.selectCar,
    );

  },

  getzhiganlist() {
    var _this = this;
    var postdata = {
      "Page": 1,
      "PageSize": 9999
    }
    http.request({
      url: 'car_zhiganServiceApi/page',
      method: "Post",
      data: postdata,
      callBack: (res) => {
        // console.log(res)
        let newArr = res.result.items;

        wx.stopPullDownRefresh()

        _this.setData({
          ZhiganArr: [...[_this.data.selectZhigan], ...newArr],
        })


      }
    })
  },

  getColorlist() {
    var _this = this;
    var postdata = {
      "Page": 1,
      "PageSize": 9999
    }
    http.request({
      url: 'car_colorServiceApi/page',
      method: "Post",
      data: postdata,
      callBack: (res) => {
        // console.log(res)
        let newArr = res.result.items;
        let totalPages = res.result.totalPages

        wx.stopPullDownRefresh()

        _this.setData({

          ColorArr: [...[_this.data.selsectColor], ...newArr],
        })


      }
    })
  },

  getColor2list() {
    var _this = this;
    var postdata = {
      "Page": 1,
      "PageSize": 9999
    }
    http.request({
      url: 'car_color2ServiceApi/page',
      method: "Post",
      data: postdata,
      callBack: (res) => {
        // console.log(res)
        let newArr = res.result.items;
        let totalPages = res.result.totalPages

        wx.stopPullDownRefresh()

        _this.setData({

          ColorArr: [...[_this.data.selsectColor], ...newArr],
        })


      }
    })
  },


  getCarlist() {
    var _this = this;

    http.request({
      url: 'car_carServiceApi/page',
      method: "Get",

      callBack: (res) => {
        // console.log(res)
        let newArr = res.result;

        wx.stopPullDownRefresh()

        _this.setData({
          CarArr: newArr,
        })


      }
    })
  },

  getSubCarlist(pid) {
    var _this = this;

    if(pid==0)
    {
      return;
    }
    var postdata = {
      "Page": 1,
      "PageSize": 9999,
      "pid": pid
    }

    http.request({
      url: 'car_carServiceApi/page',
      method: "Post",
      data: postdata,
      callBack: (res) => {
        // console.log(res)
        let newArr = res.result.items;

        wx.stopPullDownRefresh()

        _this.setData({
          'CarArr[1]': newArr,
          SubCarArr: newArr
        })


      }
    })
  },


  getXiLielist() {
    var _this = this;
    var postdata = {
      "Page": 1,
      "PageSize": 9999
    }
    http.request({
      url: 'car_xilieServiceApi/page',
      method: "Post",
      data: postdata,
      callBack: (res) => {
        // console.log(res)
        let newArr = res.result.items;
        let totalPages = res.result.totalPages

        wx.stopPullDownRefresh()

        _this.setData({
          XiLieArr: [...[_this.data.selectXiLie], ...newArr],
        })


      }
    })
  },

  bindPickerChangeZG: function (e) {
    var index = e.detail.value
    var sdata = this.data.ZhiganArr[index]
    if(sdata==undefined)
    {
      return;
    }
    this.setData({
      selectZhigan: sdata,
      datalist: [],
    })
    page = 1;

    this.getdatalist(
      sdata,
      this.data.selsectColor,
      this.data.selectCar,
    );
  },

  bindPickerChangeColor: function (e) {
    var index = e.detail.value
    var sdata = this.data.ColorArr[index]
    if(sdata==undefined)
    {
      return;
    }
    this.setData({
      selsectColor: sdata,
      datalist: [],
    })
    page = 1;

    this.getdatalist(
      this.data.selectZhigan,
      sdata,
      this.data.selectCar,
    );
  },

  bindPickerChangeCar: function (e) {

    var index = e.detail.value[1]

    var sdata = this.data.SubCarArr[index]
    if(sdata==undefined)
    {
      return;
    }
    this.setData({
      selectCar: sdata,
      datalist: [],
    })
    page = 1;

    this.getdatalist(
      this.data.selectZhigan,
      this.data.selsectColor,
      sdata,
    );
  },

  bindPickerChangeCar2: function (e) {

    if (e.detail.column > 0) {
      return;
    }
    var index = e.detail.value
 
    if(index==0)
    {
      this.setData({
        'CarArr[1]': [{
          id: 0,
          carname: "所有车型"
        }],
        SubCarArr: [{
          id: 0,
          carname: "所有车型"
        }]
      })
      return;
    }
    var sdata = this.data.CarArr[0][index]
    if(sdata==undefined)
    {
      return;
    }
    this.getSubCarlist(sdata.id)
  },

  bindPickerChangeXilie: function (e) {
    // var index=e.detail.value
    // var sdata=this.data.XiLieArr[index]
    //  this.setData({
    //   selectXiLie: sdata
    //  })
    //  page=1;
    //  this.data.datalist=[]
    //  this.getdatalist(
    //    this.data.selectCar,
    //    sdata,
    //    this.data.selectZhigan,
    //    this.data.selsectColor,
    //    this.data.searchWork
    //  );
  },

  getdatalist: function (selectZhigan, selsectColor, selectCar) {
    var _this = this;
    this.setData({
      isloading: true
    })

    var postdata = {
      "Page": page,
      "PageSize": pageSize,
      "carid": selectCar.id,
      "zhiganId": selectZhigan.id,
      "colorId": selsectColor.id,
    }
    http.request({
      url: 'car_caseServiceApi/page',
      method: "Post",
      data: postdata,
      callBack: (res) => {

        wx.stopPullDownRefresh()

        if (res.result.total == 0) {
          _this.setData({
            isloading: false
          })
      
          return;
        }
        let newArr = res.result.items;
        let totalPages = res.result.totalPages

       

        _this.setData({
          isloading: false,
          datalist: [..._this.data.datalist, ...newArr],
          isMore: page < totalPages
        })


      }
    })
  },


  onclickItem: function (e) {
    // console.log(e.currentTarget.dataset.id)
    var id = e.currentTarget.dataset.id;
    var title = e.currentTarget.dataset.title;
    wx.navigateTo({
      url: '/pages/case/details?id=' + id + '&title=' + title,
    })
  },

  onclickSearch:function()
  {
    wx.navigateTo({
      url: '/pages/search/search1',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})